variables: variables.yml

templates:
  transmission-settings:
    _transmission-primary: &transmission-primary
      host: '{? torrent.primary.host ?}'
      port: '{? torrent.primary.port ?}'
      username: '{? torrent.primary.username ?}'
      password: '{? torrent.primary.password ?}'
    _transmission-secondary: &transmission-secondary
      host: '{? torrent.secondary.host ?}'
      port: '{? torrent.secondary.port ?}'
      username: '{? torrent.secondary.username ?}'
      password: '{? torrent.secondary.password ?}'
  kodi-settings:
    _kodi-primary: &kodi-primary
      url: '{? kodi.url ?}'
      port: '{? kodi.port ?}'
      username: '{? kodi.username ?}'
      password: '{? kodi.password ?}'
  category-movies-settings:
    _category-movies-filter: &category-movies-filter
      if:
        - "not movie_name|default(None)": fail
        # Sometimes anime does not have year due the lookup are not optimized for anime
        - "not movie_year|default(None) and not 'anime' in category": fail
        - "'spanish' in location|lower":
            set:
              category: "spanish"
        - "'spanish' in location|lower and imdb_mpaa_rating|default('') in ['PG-13']":
            set:
              category: "pg13"
        - "'spanish' in location|lower and imdb_mpaa_rating|default('') in ['PG']":
            set:
              category: "pg"
        - "'spanish' in location|lower and ('family' in trakt_genres|default([]) or 'family' in imdb_genres|default([]))":
            set:
              category: "family"
        - "'general' in location|lower and ('family' in trakt_genres|default([]) or 'family' in imdb_genres|default([]))":
            set:
              category: "family_english"
        - "'spanish' in location|lower and imdb_mpaa_rating|default('') in ['G']":
            set:
              category: "allages"
        - "'general' in location|lower and imdb_mpaa_rating|default('') in ['G']":
            set:
              category: "allages_english"
        - "'spanish' in location|lower and imdb_mpaa_rating|default('') in ['R', 'NC-17']":
            set:
              category: "adults_spanish"
        - "'general' in location|lower and imdb_mpaa_rating|default('') in ['R', 'NC-17']":
            set:
              category: "adults"
        - "'anime' in location|lower or 'anime' in trakt_genres|default([]) or 'anime' in imdb_genres|default([])":
            set:
              category: "anime"
        - "'spanish' in location|lower and ('anime' in trakt_genres|default([]) or 'anime' in imdb_genres|default([]))":
            set:
              category: "anime_spanish"
        - "'spanish' in location|lower and ('anime' in trakt_genres|default([]) or 'anime' in imdb_genres|default([])) and imdb_mpaa_rating|default('') in ['G', 'PG', 'TV-PG', 'TV-G', 'TV-Y', 'TV-Y7']":
            set:
              category: "anime_pg"
  category-series-settings:
    _category-series-filter: &category-series-filter
      if:
        - "series_name is undefined or series_name is none": fail
        - "'spanish' in location|lower":
            set:
              category: "spanish"
        - "'spanish' in location|lower and tvdb_content_rating|default('') in ['G', 'PG', 'PG-13', 'TV-PG', 'TV-G', 'TV-Y', 'TV-Y7', 'TV-14']":
            set:
              category: "pg"
        - "'spanish' in location|lower and tvdb_content_rating|default('') in ['G']":
            set:
              category: "allages"
        - "'general' in location|lower and tvdb_content_rating|default('') in ['G']":
            set:
              category: "allages_english"
        - "'anime' in location|lower or 'anime' in trakt_genres|default([]) or 'anime' in tvdb_genres|default([]) or 'Anime' in tvdb_genres|default([])":
            set:
              category: "anime"
        - "'spanish' in location|lower and 'anime' in category|default('')":
            set:
              category: "anime_spanish"
        - "'spanish' in location|lower and 'anime' in category|default('') and tvdb_content_rating|default('') in ['G', 'PG', 'PG-13', 'TV-PG', 'TV-G', 'TV-Y', 'TV-Y7', 'TV-14']":
            set:
              category: "anime_pg"
  transmission-movies:
    transmission:
      <<: *transmission-primary
      path: '{? paths.torrent ?}/movies/general'
      add_paused: no
      main_file_only: yes
      rename_like_files: yes
      magnetization_timeout: 120
      content_filename: "{{ movie_name }}{% if movie_year|default(None) %} ({{ movie_year }}){% endif %}{% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}{% if tmdb_id|default(None) %}.tmdb-{{tmdb_id}}{% elif imdb_id|default(None) %}.imdb-{{imdb_id}}{% endif %}"
      include_subs: yes
      skip_files:
        - '*.sfv'
        - '*[sS]ample*'
        - '*.txt'
        - '*.lnk'
  transmission-purge:
    disable: [seen, seen_info_hash]
    regexp:
      reject_excluding:
        - /+(downloads|Torrents)/+(anime|movies|series)
      from: transmission_downloadDir
  language-reject:
    regexp:
      reject:
        - \b(ita|ger|spa|swe|nor|nordic|kor|esp|nl|pl|french|casteñano|spanish|hindi)\b # Languages
        - \b(un|hard)sub|sub(bed|titulado)\b # Subtitles (hardsub / Subbed / Subtitulado / Unsubbed) allow Subs|Sub
        - \b(dub|_dub|dubbed)\b
        - \bdublat\n
        - \bVOSTFR\b
      from: title
  common-reject:
    regexp:
      reject:
        - \b60fps\b
        - \bpv\b
        - \bHC\b
        - 1XBET
        - \bpctreload\b
        - \bpctmix\b
        - ganool
      from: title
  common-sort:
    max_reruns: 1
    sort_by:
      - field: torrent_availability
        reverse: yes
      - field: quality
        reverse: yes
  common-filter:
    convert_magnet:
      timeout: 180 seconds
    #magnets: no
    content_filter:
      require:
        - '*.avi'
        - '*.mkv'
        - '*.mp4'
      reject: '*.lnk'
  common-urlrewrite:
    urlrewrite:
      #torrentz2:
      #  regexp: 'https?://torrentz2.eu/(?P<id>\w+)'
      #  format: 'magnet:?xt=urn:btih:\g<id>'
      # itorrents:
      #   regexp: 'https?://itorrents.org/torrent/(?P<id>\w+)\.torrent.*'
      #   format: 'magnet:?xt=urn:btih:\g<id>'
      torrentdownload:
        regexp: 'https?://www.torrentdownload.info/.*/(?P<id>\w+)'
        format: 'magnet:?xt=urn:btih:\g<id>'
  lookup-movies:
    trakt_lookup: yes
    imdb_lookup: yes
    tmdb_lookup: yes
    parsing:
      movie: guessit
  copy-series:
    <<: *category-series-filter
  copy-movies:
    <<: *category-movies-filter
    manipulate:
      - imdb_id:
          from: filename
          extract: '.*imdb-(.*)\..*'
          find_all: yes
      - tmdb_id:
          from: filename
          extract: '.*tmdb-(.*)\..*'
          find_all: yes
    content_size:
      min: 275
    require_field:
      - movie_name
    exists_movie:
      path: '{? paths.movies ?}'
      allow_different_qualities: better
      type: files
    copy:
      to: "{? paths.movies ?}/{{ category|default('general')|lower }}/{{ trakt_movie_name|default(movie_name) }}{% if movie_year|default(None) %} ({{ movie_year }}){% endif %}"
      rename: "{{ trakt_movie_name|default(movie_name) }} {% if movie_year|default(None) %}({{ movie_year }}){% endif %}{% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}"
      allow_dir: yes
      along:
        subdirs:
          - Sub
          - Subs
          - Extra
          - Extras
        extensions:
          - srt
  download-movies:
    proper_movies: 4 weeks
    domain_delay:
      torrentz2: 5 seconds
      zooqle: 5 seconds
      thepiratebay: 5 seconds
      apibay: 5 seconds
      limetorrents: 5 seconds
    torrent_alive: 1 # Will reject results with less than 1 seeds
    content_size:
      min: 200
      max: 9500
  subtitles-movies:
    list_add:
      - subtitle_list:
          list: subtitles-movies
          check_subtitles: yes
          remove_after: 7 days
          languages:
            - es
            - en
  manipulate-anime:
    manipulate:
      - title:
          replace:
            regexp: '[^a-zA-Z0-9\-~_.()\[\]\s]' # remove katakana or similar to better parsing
            format: ''
      - title:
          replace:
            regexp: '([a-zA-Z]+)\s+?-\s+?([a-zA-Z]+)'
            format: '\1 \2'
      - title:
          replace:
            regexp: 'Season\s?(\d{1,3})\s?(\(\d+\))?\s?-?\s?(\d{2,3})'
            format: '\2 S\1E\3'
      - title:
          replace:
            regexp: 'S(\d{2,3})-?E(\d{2,3})'
            format: 'S\1E\2'
      - title:
          replace:
            regexp: '(\d{1,3})\s+?~\s+?(\d{1,3})'
            format: '\1-\2'
      - title:
          replace:
            regexp: 'SP\s+?(\d{1,3})'
            format: 'S00E\1'
      - title:
          replace:
            regexp: '\sTV\s|--+'
            format: '-'
      - title:
          replace:
            regexp: '(\d{1,3})v\d{1,2}'
            format: '\1'
  kodi-update:
    kodi_library:
      <<: *kodi-primary
      action: scan
      category: video
  kodi-clean:
    kodi_library:
      <<: *kodi-primary
      action: clean
      category: video
  notify-all:
    notify:
      entries:
        title: '*Task:* {{task}} | {{title}}'
        message: |
          {%- if trakt_series_name is defined or series_name is defined %}
          *Starting to donwload serie:* {{trakt_series_name|d(series_name)}} - {{trakt_ep_id|d(series_id)}}
          *Season:* {{trakt_season|d(series_season)}}
          *Episode:* {{trakt_episode|d(series_id)}}
          *Name:* {{trakt_ep_name|d('N/A')}}
          {%- elif movie_name is defined %}
          *Starting to download movie:* {{ movie_name }} ({{ movie_year|d('--') }})
          {%- else %}
          *Title:* {{title}}
          {%- endif %}
          *Quality:* {{quality|d('')}}
          *Category:* {{category|d('english')|capitalize}}
          {%- if imdb_mpaa_rating is defined and imdb_mpaa_rating %}
          *Rating:* {{imdb_mpaa_rating|default('')}}
          {%- endif %}
          {%- if trakt_rating is defined and trakt_rating %}
          *Score trakt:* {{trakt_rating}}/10 - {{trakt_votes|d('-')}} votes
          {%- endif %}
          {%- if tmdb_rating is defined and tmdb_rating %}
          *Score tmdb:* {{tmdb_rating}}/10 - {{tmdb_votes|d('-')}} votes
          {%- endif %}
          {%- if imdb_id is defined %}
          {%- if imdb_score is defined and imdb_score %}
          *Score imdb:* {{imdb_score}}/10 - {{imdb_votes|d('-')}} votes
          {%- endif %}
          *Genres:* {{imdb_genres|join(', ')|title if imdb_genres is defined and imdb_genres else '-'}}
          *Directors:* {{imdb_directors.values()|join(', ')|title if imdb_directors is defined and imdb_directors else '-'}}
          {%- endif %}
          {%- if trakt_series_url is defined %}
          *Url:* {{trakt_series_url}}
          {%- elif imdb_url is defined %}
          *Url:* {{imdb_url}}
          {%- endif %}
          {%- if trakt_overview is defined %}
          *Plot:*
          {%- if trakt_translations is defined and trakt_translations and 'es' in trakt_translations %}
          _[Español]_ {{trakt_translations.es.overview}}
          -----------
          {%- endif %}
          _[English]_ {{trakt_overview|d(imdb_plot_outline|d(''))}}
          {%- endif %}
          {%- if trakt_trailer is defined %}
          -----------
          [Trailer:]({{trakt_trailer}})
          {%- endif %}
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'
          - telegram:
              bot_token: '{? telegram.token ?}'
              parse_mode: markdown
              disable_previews: no
              recipients: '{? telegram.recipients ?}'

tasks:
  collection-movies:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: collection
          type: movies
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: collection
          type: movies
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies

  # task to pull movies from trakt.tv and add to the movie list
  watchlist-movies:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: watchlist
          type: movies
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: watchlist
          type: movies
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies

  watchlist-movies-anime:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - anilist:
          username: '{? accounts.anilist_primary.username ?}'
          status:
            - current
            - planning
            - completed
          release_status:
            - releasing
            - finished
          format:
            - movie
      - anidb_list:
          user_id: '{? accounts.anidb_primary.username ?}'
          type: movies
          mode: get
      - anidb_list:
          user_id: '{? accounts.anidb_primary.username ?}'
          type: movies
          mode: watch
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies_anime

  watchlist-movies-spanish:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: movies
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: spanish
          type: movies
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies_spanish

  watchlist-movies-spanish-all:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: movies
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: spanish
          type: movies
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: watchlist
          type: movies
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies_spanish_all

  watchlist-movies-spanish-force:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - trakt_list:
          username: '{? accounts.trakt_secondary.username ?}'
          list: spanish
          type: movies
          language: es
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: movies
          language: es
    accept_all: yes
    list_add:
      - movie_list: watchlist_movies_spanish_force

  # tasks to pull series lists
  watchlist-series:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      # - trakt_list:
      #     username: '{? accounts.trakt_secondary.username ?}'
      #     list: watchlist
      #     type: shows
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: watchlist
          type: shows
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: series
          type: shows
    set_series_begin: yes
    if:
      - "series_name is defined": accept
    list_add:
      - entry_list: watchlist_series

  watchlist-series-anime:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      - anidb_list:
          user_id: '{? accounts.anidb_primary.username ?}'
          type: shows
          mode: get
      - anidb_list:
          user_id: '{? accounts.anidb_primary.username ?}'
          type: ovas
          mode: get
      - anilist:
          username: '{? accounts.anilist_primary.username ?}'
          status:
            - current
            - planning
            - completed
          release_status:
            - releasing
            - finished
          format:
            - ona
            - tv_short
            - special
    set_series_begin: yes
    if:
      - "anidb_name is defined": accept
      - "al_title is defined": accept
    list_add:
      - entry_list: watchlist_series_anime

  watchlist-series-spanish:
    priority: 1
    seen: local  # We don't want accepted movies on this feed to affect actual download feed
    inputs:
      # - trakt_list:
      #     username: '{? accounts.trakt_secondary.username ?}'
      #     list: spanish
      #     type: shows
      - trakt_list:
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: shows
    set_series_begin: yes
    if:
      - "series_name is defined": accept
    list_add:
      - entry_list: watchlist_series_spanish

  # collect next movies to download
  # with a primitive queue
  collect-movies:
    manual: yes
    priority: 1
    seen: no
    accept_all: yes
    movie_list: watchlist_movies
    limit_new: 20
    sort_by:
      - field: added
        reverse: no
    list_add: # update added date to send it to last position
      - movie_list: watchlist_movies

  collect-movies-spanish:
    manual: yes
    priority: 1
    seen: no
    accept_all: yes
    movie_list: watchlist_movies_spanish
    limit_new: 20
    sort_by:
      - field: added
        reverse: no
    list_add: # update added date to send it to last position
      - movie_list: watchlist_movies_spanish

  collect-movies-spanish-modified:
    manual: yes
    priority: 1
    seen: no
    accept_all: yes
    movie_list: watchlist_movies_spanish
    limit_new: 20
    sort_by:
      - field: added
        reverse: no
    manipulate:
      - title:
          phase: metainfo
          replace:
            regexp: '(\(.*\))'
            format: '\1 lat'

  collect-movies-spanish-force:
    manual: yes
    priority: 1
    seen: no
    accept_all: yes
    movie_list: watchlist_movies_spanish_force
    limit_new: 20
    sort_by:
      - field: added
        reverse: no
    list_add: # update added date to send it to last position
      - movie_list: watchlist_movies_spanish_force

  # task to copy finished movies
  copy-movies:
    priority: 2
    template:
      - lookup-movies
      - copy-movies
      - subtitles-movies
      - notify-all
      - kodi-update
    metainfo_movie: yes
    accept_all: yes
    seen: local
    filesystem:
      path:
        - '{? paths.torrent ?}/movies'
        - '{? paths.torrent ?}/remote/movies'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    digest:
      list: copy_movies_fallback
      state:
        - failed

  copy-movies-fallback:
    priority: 3
    template:
      - lookup-movies
      - copy-movies
      - subtitles-movies
      - notify-all
      - kodi-update
    metainfo_movie: no
    parsing:
      movie: internal
    manipulate:
      - imdb_id:
          remove: yes
      - imdb_name:
          remove: yes
      - imdb_year:
          remove: yes
      - imdb_url:
          remove: yes
      - trakt_movie_id:
          remove: yes
      - trakt_movie_name:
          remove: yes
      - tmdb_id:
          remove: yes
      - movie_year:
          remove: yes
      - movie_name:
          remove: yes
      # - movie_year:
      #     from: title
      #     extract: \((\d+)\)
      # - movie_name:
      #     from: title
      #     extract: (.*)\(\d+\)
      - title:
          replace:
            regexp: '[\-~_\s]'
            format: ' '
      - title:
          replace:
            regexp: '[\[\]\.]'
            format: '.'
    retry_failed: no
    accept_all: yes
    seen: local
    require_field:
      - movie_name
      - movie_year
    unique:
      field:
        - id
        - title
      action: reject
    from_digest:
      list: copy_movies_fallback
    sequence:
      - notify:
          entries:
            what: failed
            title: '* Failed Copy Movie: {{movie_name|default(title)}} ({{movie_year|default("--")}})'
            message: |
              Task: {{task}}
              Id: {{id|default(title)}}
              Filename: {{filename}}
              Location: {{location}}
              Reason: {{reason|default("")}}
            via:
              - xmpp:
                  sender: '{? xmpp.sender ?}'
                  password: '{? xmpp.password ?}'
                  recipients: '{? xmpp.recipients ?}'

  # task to copy finished series
  copy-series:
    priority: 2
    template:
      - copy-series
      - kodi-update
    metainfo_series: yes
    trakt_lookup: yes
    thetvdb_lookup: yes
    parsing:
      series: guessit
    accept_all: yes
    seen: local
    filesystem:
      path:
        - '{? paths.torrent ?}/series/general'
        - '{? paths.torrent ?}/series/spanish'
        - '{? paths.torrent ?}/remote/series/general'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    if:
      # Delegate to series-copy-anime task
      - "'anime' in category|default('')": reject
      - "trakt_series_name|default(None) is none and tvdb_series_name|default(None) is none": reject
    content_size:
      min: 20
    require_field:
      - series_id
      - series_name
      - series_season
    exists_series:
      path: "{? paths.series ?}/{{ category|default('general')|lower }}/{{ trakt_series_name|default(series_name)|title }}/Season {{ trakt_season|default(series_season) }}"
      allow_different_qualities: better
    copy:
      to: "{? paths.series ?}/{{ category|default('general')|lower }}/{{ trakt_series_name|default(series_name)|title }}/Season {{ trakt_season|default(series_season)  }}"
      rename: "{{ trakt_series_name|default(series_name)|title }} {{ trakt_ep_id|default(series_id) }}{%if trakt_ep_name|default(False) %}-{{ trakt_ep_name }}{% endif %}{% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}"
      allow_dir: yes
      along:
        subdirs:
          - Sub
          - Subs
          - subs
          - sub
          - Subs/
          - Sub/
          - Extra
          - Extras
        extensions:
          - srt
    digest:
      list: purge_series
      state:
        - rejected
    list_add:
      - subtitle_list:
          list: subtitles-series
          remove_after: 7 days
          allow_dir: true
          recursion_depth: 2
          languages:
            - es
            - en

  # task to copy finished anime series
  copy-series-anime:
    priority: 2
    template:
      - copy-series
      - manipulate-anime
      - kodi-update
    metainfo_series: yes
    thetvdb_lookup: yes
    parsing:
      series: guessit
    accept_all: yes
    seen: local
    filesystem:
      path:
        - '{? paths.torrent ?}/series/anime'
        - '{? paths.torrent ?}/series/spanish'
        - '{? paths.torrent ?}/remote/series/anime'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    if:
      - "'anime' not in category|default('')": reject
      - "series_parser is defined and series_parser.season is not none and series_parser.season|string|length < 4 and series_id is number":
          set:
            anime_season: "{{series_parser.season}}"
            anime_episode: "{{series_parser.season}}E{{series_id}}"
      - "series_id is string and series_id|default('')|length > 7":
          set:
            anime_episode: "{{series_id|re_replace('S[0-9]+E', 'EP')}}"
      - "tvdb_first_air_date|default(None) is not none":
          set:
            anime_first_air_date: " ({{tvdb_first_air_date|formatdate('%Y')}})"
      - "series_parser is defined and tvdb_series_name|default(None) is not none and series_parser.name != tvdb_series_name":
          # This is because any of the flexget lookup support anime correctly
          set:
            tvdb_series_name: ""
            tvdb_ep_name: ""
            anime_first_air_date: ""
      - "series_parser is defined and series_parser.year|default(None) is not none and series_parser.year|string != anime_first_air_date|default('')":
          # TODO: implement anidb lookup
          set:
            anime_year: " ({{series_parser.year}})"
      - "series_name is defined":
          set:
            anime_title: "{{ tvdb_series_name|default(series_name, true)|strip_year|title }}{{ anime_first_air_date|default(anime_year|default('')) }}"
            anime_path: "{{ category|default('anime')|lower }}/{{ anime_title }}{%if tvdb_season|default(anime_season|default(False)) %}/Season {{ tvdb_season|default(anime_season) }}{% endif %}"
    content_size:
      min: 20
    require_field:
      - series_id
      - series_name
    exists_series:
      path: "{? paths.series ?}/{{anime_path}}"
      allow_different_qualities: better
    copy:
      to: "{? paths.series ?}/{{anime_path}}"
      rename: "{{ anime_title }} {%if tvdb_ep_id|default(False) %}{{ tvdb_ep_id|upper }}{% elif tvdb_episode|default(False) %}{{ \"EP\" ~ tvdb_episode }}{% elif series_id is number %}{{ anime_episode|default(\"EP\" ~ series_id) }}{% else %}{{ anime_episode|default(series_id)|upper }}{% endif %}{%if tvdb_ep_name|default(False, true) %}-{{ tvdb_ep_name }}{% endif %}{% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}"
      allow_dir: yes
      along:
        subdirs:
          - Sub
          - Subs
          - Extra
          - Extras
        extensions:
          - srt
    list_add:
      - subtitle_list:
          list: subtitles-series
          remove_after: 7 days
          languages:
            - es
            - en

  # task that automatically downloads movies from the movie_list
  download-movies:
    priority: 10
    template:
      - lookup-movies
      - common-sort
      - common-filter
      - common-reject
      - language-reject
      - common-urlrewrite
      - download-movies
      - transmission-movies
    discover:
      what:
        - from_task: collect-movies
      from:
        - 1337x:
            order_by: seeders
        - piratebay:
            category: highres movies
            sort_by: seeds
            sort_reverse: false
        - piratebay:
            category: movies
            sort_by: seeds
            sort_reverse: false
        - rarbg:
            category:
              - x264 720p
              - Full BD
              - XviD 720p
            sorted_by: seeders
            min_seeders: 1
            use_tvdb: true
        - rarbg:
            category:
              - x264 720p
              - Full BD
              - XviD 720p
            sorted_by: seeders
            min_seeders: 1
        - yts: yes
        - argenteam:
            force_subtitles: no
        #- torrentleech:
        #    rss_key: '{? accounts.torrentleech.key ?}'
        #    username: '{? accounts.torrentleech.username ?}'
        #    password: '{? accounts.torrentleech.password ?}'
      interval: 2 hours
      release_estimations: ignore
    # best_quality:
    #   on_best: do_nothing
    #   on_lower: reject
    quality: hr-720p webrip+ !h265 !10bit !dts !dtshd
    upgrade:
      target: 720p webdl+ !h265 !10bit !dts !dtshd
      tracking: yes
      timeframe: 8 weeks
      on_lower: reject
      propers: yes
    list_match:
      from:
        - movie_list: watchlist_movies
    require_field:
      - movie_name
      - movie_year
    digest:
      list: clean_watchlist
      state:
        - accepted

  download-movies-anime:
    priority: 10
    template:
      - common-sort
      - common-filter
      - common-reject
      - language-reject
      - common-urlrewrite
      - download-movies
      - transmission-movies
    metainfo_movie: yes
    parsing:
      movie: guessit
    domain_delay:
      nyaa: 20 seconds
    discover:
      what:
        - movie_list:
            list_name: watchlist_movies_anime
            strip_year: no
      from:
        - nyaa:
            category: anime raw
            filter: filter remakes
        - nyaa:
            category: anime eng
            filter: filter remakes
        - 1337x:
            category: anime
            order_by: seeders
        - limetorrents:
            category: anime
            order_by: seeds
      interval: 2 hours
      release_estimations: ignore
    manipulate:
      - title:
          replace:
            regexp: '[^a-zA-Z0-9\-_.()\[\]\s]' # remove katakana or similar
            format: ''
      - title:
          replace:
            regexp: '^\[(.*?)\]\[(.*?)\](\[|\()'
            format: '[\1]\2\3'
    # best_quality:
    #   identified_by: "{{media_id|default(discovered_from)}}"
    #   on_best: do_nothing
    #   on_lower: reject
    quality:
      - dvdrip+
      - hr-720p webrip+ !10bit !dts !dtshd
      - hr-1080p !cam !ts !bdscr !dvdscr !10bit !dts !dtshd
      - webrip+ !cam !ts !bdscr !dvdscr !10bit !dts !dtshd
    upgrade:
      target: 1080p webdl+ !10bit !dts !dtshd
      tracking: yes
      timeframe: 8 weeks
      on_lower: reject
      propers: yes
    list_match:
      from:
        - movie_list: watchlist_movies_anime
    require_field:
      - movie_name
    set:
      path: '{? paths.torrent ?}/movies/anime'
    digest:
      list: clean_watchlist_anime
      state:
        - accepted

  # task that automatically downloads movies in spanish
  download-movies-spanish:
    priority: 10
    template:
      - lookup-movies
      - common-sort
      - common-filter
      - common-reject
      - common-urlrewrite
      - download-movies
      - transmission-movies
    discover:
      what:
        - from_task: collect-movies-spanish
        - from_task: collect-movies-spanish-force
      from:
        #- torrentz:
        #    reputation: good
        #    extra_terms: lat
        #- torrentz:
        #     reputation: good
        #     extra_terms: latino
        #- torrentz:
        #     reputation: good
        #     extra_terms: cinecalidad
        # - descargas2020: yes
        - argenteam:
            force_subtitles: no
        #- torrentleech:
        #    rss_key: '{? accounts.torrentleech.key ?}'
        #    username: '{? accounts.torrentleech.username ?}'
        #    password: '{? accounts.torrentleech.password ?}'
        #- search_rss:
        #    url: 'https://zooqle.com/search?q={{ search_term }}+lat&fmt=rss'
        #    link: 'auto'
        #- search_rss:
        #    url: 'https://zooqle.com/search?q={{ search_term }}+latino&fmt=rss'
        #    link: 'auto'
        # TODO: custom plugins for torrentdownloads/cinecalidad and elitetorrent.nl
        #- search_rss:
        #    url: 'https://www.torrentdownloads.me/rss.xml?type=search&search={{ search_term }}+lat'
        #    link: 'info_hash'
        #- search_rss:
        #    url: 'https://www.torrentdownloads.me/rss.xml?type=search&search={{ search_term }}+latino'
        #    link: 'info_hash'
        - search_rss:
            url: 'https://www.torrentdownload.info/feed?q={{search_term}}+lat'
            link: 'auto'
        - search_rss:
            url: 'https://www.torrentdownload.info/feed?q={{search_term}}+latino'
            link: 'auto'
        - search_rss:
            url: 'https://www.limetorrents.info/searchrss/{{search_term}}+lat'
            link: 'auto'
        - search_rss:
            url: 'https://www.limetorrents.info/searchrss/{{search_term}}+latino'
            link: 'auto'
      interval: 2 hours
      release_estimations: ignore
    quality:
      - dvdrip+ !h265 !10bit !dts !dtshd
      - 480p-1080p !cam !ts !bdscr !dvdscr !h265 !10bit !dts !dtshd
      - 720p+ webrip+ !h265 !10bit !dts !dtshd
    # best_quality:
    #   on_best: do_nothing
    #   on_lower: reject
    upgrade:
      target: 1080p webdl+ !h265 !10bit !dts !dtshd
      tracking: yes
      timeframe: 8 weeks
      on_lower: reject
      propers: yes
    regexp:
      accept:
        - \bcinecalidad\b
      reject_excluding:
        - .lat|-lat-|latino|latin|dual-lat|dual.lat|dual-latino|spa-lat|spa-latino|spanish-latino|esp-lat|esp-latino|esp.lat|spa.lat
      from:
        - title
    list_match:
      from:
        - movie_list: watchlist_movies_spanish
        - movie_list: watchlist_movies_spanish_all
        - movie_list: watchlist_movies_spanish_force
    require_field:
      - movie_name
      - movie_year
    set:
      path: '{? paths.torrent ?}/movies/spanish'
    digest:
      list: clean_watchlist_spanish
      state:
        - accepted

  download-movies-spanish-optimistic:
    priority: 10
    template:
      - lookup-movies
      - common-sort
      - common-filter
      - common-reject
      - common-urlrewrite
      - download-movies
      - transmission-movies
    discover:
      what:
        - from_task: collect-movies-spanish-modified
        - from_task: collect-movies-spanish-force
      from:
        - 1337x:
            order_by: seeders
        - piratebay:
            category: highres movies
            sort_by: seeds
            sort_reverse: false
        - piratebay:
            category: movies
            sort_by: seeds
            sort_reverse: false
        - rarbg:
            category:
              - x264
              - x264 720p
              - x264 1080p
              - XviD
              - XviD 720p
              - Full BD
            sorted_by: seeders
            min_seeders: 1
        - argenteam:
            force_subtitles: no
        - limetorrents:
            category: movies
            order_by: seeds
      interval: 2 hours
      release_estimations: ignore
    quality:
      - dvdrip+ !h265 !10bit !dts !dtshd
      - 480p-1080p !cam !ts !bdscr !dvdscr !h265 !10bit !dts !dtshd
      - 720p+ webrip+ !h265 !10bit !dts !dtshd
    upgrade:
      target: 1080p webdl+ !h265 !10bit !dts !dtshd
      tracking: yes
      timeframe: 8 weeks
      on_lower: reject
      propers: yes
    regexp:
      accept:
        - \bcinecalidad\b
      reject_excluding:
        - .lat|-lat-|latino|latin|dual-lat|dual.lat|dual-latino|spa-lat|spa-latino|spanish-latino|esp-lat|esp-latino|esp.lat|spa.lat
      from:
        - title
    list_match:
      from:
        - movie_list: watchlist_movies_spanish
        - movie_list: watchlist_movies_spanish_all
        - movie_list: watchlist_movies_spanish_force
    require_field:
      - movie_name
      - movie_year
    set:
      path: '{? paths.torrent ?}/movies/spanish'
    digest:
      list: clean_watchlist_spanish
      state:
        - accepted

  # task that automatically downloads series from the entry_list
  download-series:
    priority: 10
    template:
      - common-sort
      - common-filter
      - common-reject
      - language-reject
      - common-urlrewrite
      - notify-all
    trakt_lookup: yes
    thetvdb_lookup: yes
    parsing:
     series: guessit
    domain_delay:
      1337x: 5 seconds
      thepiratebay: 5 seconds
    discover:
      what:
        - next_series_episodes:
            from_start: yes
            backfill: yes
      from:
        - 1337x:
            order_by: seeders
        - rarbg:
            category:
              - 49 #  TV UHD Episodes
              - HDTV
              - SDTV
            sorted_by: seeders
            min_seeders: 1
        - rarbg:
            category:
              - 49 #  TV UHD Episodes
              - HDTV
              - SDTV
            sorted_by: seeders
            min_seeders: 1
            use_tvdb: yes
        - piratebay:
            category: highres tv
            sort_by: seeds
            sort_reverse: false
        - piratebay:
            category: tv
            sort_by: seeds
            sort_reverse: false
        - limetorrents:
            category: tv
            order_by: seeds
      interval: 2 hours
      release_estimations:
        optimistic: 90 days
    torrent_alive: 1 # Will reject results with less than 1 seeds
    content_size:
      min: 20
      max: 7500
    include: series.yml
    configure_series:
      from:
        entry_list: watchlist_series
        # limit:
        #   amount: 2
        #   from:
        #     entry_list: watchlist_series
      settings:
        identified_by: ep
        timeframe: 48 hours
        target: hr-720p hdtv+ !h265 !10bit !dts !dtshd
        quality: webrip+ !h265 !10bit !dts !dtshd
        tracking: backfill
        upgrade: yes
        specials: yes
        exact: yes
        season_packs: no
    require_field:
      - trakt_series_name
      - trakt_ep_id
    transmission:
      <<: *transmission-secondary
      path: '{? paths.torrent_remote ?}/series/general'
      add_paused: no
      main_file_only: yes
      rename_like_files: yes
      magnetization_timeout: 120
      content_filename: "{{ trakt_series_name }} {{ trakt_ep_id }}{%if trakt_ep_name|default(False) %}-{{trakt_ep_name}}{% endif %}{% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}"
      include_subs: yes
      skip_files:
        - '*.sfv'
        - '*[sS]ample*'
        - '*.txt'
        - '*.lnk'

  # task that automatically downloads series pack from the entry_list
  download-series-pack:
    priority: 10
    template:
      - common-sort
      - common-filter
      - common-reject
      - language-reject
      - common-urlrewrite
      - notify-all
    imdb_lookup: yes
    trakt_lookup: yes
    parsing:
     series: guessit
    domain_delay:
      1337x: 5 seconds
      thepiratebay: 5 seconds
    discover:
      what:
        - next_series_seasons:
            from_start: yes
            backfill: yes
      from:
        - 1337x:
            order_by: seeders
        - rarbg:
            category:
              - x264 720p
              - HDTV
              - SDTV
            sorted_by: seeders
            min_seeders: 1
        - rarbg:
            category:
              - x264 720p
              - HDTV
              - SDTV
            sorted_by: seeders
            min_seeders: 1
            use_tvdb: yes
        - piratebay:
            category: highres tv
            sort_by: seeds
            sort_reverse: false
        - piratebay:
            category: tv
            sort_by: seeds
            sort_reverse: false
        - limetorrents:
            category: tv
            order_by: seeds
      interval: 2 hours
      release_estimations:
        optimistic: 90 days
    torrent_alive: 2 # Will reject results with less than 1 seeds
    content_size:
      min: 20
      max: 50000
    include: series.yml
    configure_series:
      from:
        entry_list: watchlist_series
      settings:
        identified_by: ep
        timeframe: 48 hours
        target: 720p hdtv+ !h265 !10bit !dts !dtshd
        quality: webrip+ !h265 !10bit !dts !dtshd
        tracking: backfill
        upgrade: yes
        specials: yes
        exact: yes
        season_packs: only
    require_field:
      - trakt_series_name
    transmission:
      <<: *transmission-secondary
      path: '{? paths.torrent_remote ?}/series/general'
      add_paused: no
      magnetization_timeout: 120
      include_subs: yes
      skip_files:
        - '*.sfv'
        - '*[sS]ample*'
        - '*.txt'
        - '*.lnk'

  # task that automatically downloads series anime from the entry_list
  download-series-anime:
    priority: 10
    template:
      - common-sort
      - common-filter
      - common-reject
      - language-reject
      - common-urlrewrite
      - manipulate-anime
      - notify-all
    metainfo_series: yes
    thetvdb_lookup: yes
    parsing:
      series: guessit
    domain_delay:
      nyaa: 20 seconds
    discover:
      what:
        - next_series_episodes: yes
          # backfill: yes -> has error with sequence (without season) anime
      from:
        - nyaa:
            category: anime raw
            filter: filter remakes
        - nyaa:
            category: anime eng
            filter: filter remakes
      interval: 2 hours
      release_estimations: ignore
    torrent_alive: 1 # Will reject results with less than 1 seeds
    regexp:
      reject:
        # - \d\d\s?~\s?\d\d+ # Ignore packs due in sequence is not supported
        # - \d\d\-\d\d+
        - vol(\s|.)?\d+\s?-\s?vol
        - \bvol(\s|.)?\d+\b
        - \b(ita|ger|spa|swe|nor|nordic|kor|esp|nl|pl|french|casteñano|spanish|hindi)\b # Languages
        - \b(hard(sub|dub)|dub|_dub)(bed)?\b # Subtitles (hardsub / Dubbed) allow Subs|Sub
        - dub|_dub|dubbed
        - \b60fps\b
        - \bs(\d+)\s\b
        - \bpv\b
        - dublat
        - \bVOSTFR\b
        - 1XBET
        - ganool
      from: title
    content_size:
      min: 20
      max: 75000
    include: anime.yml
    configure_series:
      from:
        # limit:
        #   amount: 2
        #   from:
        entry_list: watchlist_series_anime
      settings:
        identified_by: sequence
        sequence_regexp: '(\d{2,3})(?:v(?P<version>\d))?'
        timeframe: 72 hours
        target: hr-720p hdtv+ !h265 !10bit !dts !dtshd
        qualities:
          - dvdrip+ !h265 !10bit !dts !dtshd
          - webrip+ !h265 !10bit !dts !dtshd
          - 480p-720p webrip+ !h265 !10bit <=flac
          - 480p-720p !cam !ts !bdscr !dvdscr !h265 !10bit !dts !dtshd
        upgrade: yes
        tracking: yes
        specials: yes
        begin: 01
        exact: yes
        season_packs: yes
        special_ids:
          - OVA
          - SP
          - Special
          - special
    require_field:
      # - series_id
      - series_name
    transmission:
      <<: *transmission-secondary
      path: '{? paths.torrent_remote ?}/series/anime'
      add_paused: no
      main_file_only: no
      magnetization_timeout: 120
      include_subs: yes
      skip_files:
        - '*.sfv'
        - '*[sS]ample*'
        - '*.txt'
        - '*.lnk'

  # Task to get subtitles
  subtitles-add:
    priority: 1
    manual: yes
    template:
      - notify-all
    filesystem:
      path:
        - '{? paths.movies ?}'
        - '{? paths.series ?}'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    age:
      field: modified
      action: reject
      age: 7 days
    seen: local # seen shouldn't interfer with anything outside this subtitles task
    check_subtitles: yes
    if:
      - "not subtitles or ('es' not in subtitles and 'spa' not in subtitles)": accept
    list_add:
      - subtitle_list:
          list: subtitles-all
          remove_after: 7 days
          languages:
            - es
            - en

  subtitles-get:
    priority: 1
    seen: no
    inputs:
      - subtitle_list:
          list: subtitles-series
      - subtitle_list:
          list: subtitles-movies
      - subtitle_list:
          list: subtitles-all
    list_match:
      from:
        - subtitle_list:
            list: subtitles-series
        - subtitle_list:
            list: subtitles-movies
        - subtitle_list:
            list: subtitles-all
    subliminal:
      languages:
        - es
        - en
      exact_match: yes
      single: no
      authentication:
        opensubtitles:
          username: '{? accounts.opensubtitles.username ?}'
          password: '{? accounts.opensubtitles.password ?}'

  filter-duplicate-movies:
    priority: 15
    seen: no # Too keep looking duplicates for same movie
    metainfo_movie: yes
    filesystem:
      path:
        - '{? paths.movies ?}'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
      retrieve: files
    sequence:
      - manipulate:
          - title:
              phase: filter
              from: location
      - sort_by: id
      - duplicates:
          field: id
          action: accept
      - set:
          duplicate_reason: "{{reason}}"
      - best_quality:
          on_best: reject
          on_lower: accept
    list_match: # Check is already added to avoid notification noise
      from:
        - entry_list: duplicate_movies_added
      action: reject
      remove_on_match: no
      single_match: no
    list_add:
      - pending_list: duplicate_movies
      - entry_list: duplicate_movies_added
    notify:
      entries:
        title: '* Duplicate Movies: {{movie_name|default(title)}} ({{movie_year|default("--")}})'
        message: |
          Task: {{task}}
          Id: {{id|default(title)}}
          Filename: {{filename}}
          Location: {{location}}
          Reason: {{duplicate_reason|default(reason)}}
          Status: Pending approve
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  filter-remove-movies:
    priority: 15
    seen: no # Too keep looking to remove for same movie
    filesystem:
      path:
        - '{? paths.movies ?}'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
      retrieve: files
    age:
      field: modified
      action: accept
      age: 1000 days # TODO: change after first run
    trakt_lookup:
      username: '{? accounts.trakt_primary.username ?}'
      account: '{? accounts.trakt_primary.username ?}'
    if:
      - trakt_collected: reject
      - not trakt_watched: reject
      - trakt_movie_user_rating and trakt_movie_user_rating <= 5: accept
    list_match: # Check is already added to avoid notification noise
      from:
        - entry_list: remove_movies_added
      action: reject
      remove_on_match: no
      single_match: no
    list_add:
     - pending_list: remove_movies
     - entry_list: remove_movies_added
    notify:
      entries:
        title: '* To Remove Movies: {{movie_name|default(title)}} ({{movie_year|default("--")}})'
        message: |
          Task: {{task}}
          Id: {{id|default(title)}}
          Filename: {{filename}}
          Location: {{location}}
          Rating: {{trakt_movie_user_rating}}
          Collected: {{trakt_collected}}
          Reason: {{reason|default("--")}}
          Status: Pending approve
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  filter-duplicate-series:
    priority: 15
    seen: no # Too keep looking duplicates for same movie
    metainfo_series: yes
    parsing:
      series: internal
    filesystem:
      path:
        - '{? paths.series ?}'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    sequence:
      - require_field:
          - series_id
          - series_name
          - series_season
          - series_episode
      - sort_by: id
      - duplicates:
          field: id
          action: accept
      - set:
          duplicate_reason: "{{reason}}"
      - best_quality:
          on_best: reject
          on_lower: accept
    list_add:
      - entry_list: duplicate_series
    notify:
      entries:
        title: '* Duplicate serie episode: {{series_name}} - {{series_id}}'
        message: |
          Task: {{task}}
          Id: {{id|default(title)}}
          Episode: {{series_id}}
          Filename: {{filename}}
          Location: {{location}}
          Reason: {{duplicate_reason|default(reason)}}
          Status: approved
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  filter-reject-copy-series:
    priority: 15
    metainfo_series: yes
    trakt_lookup: yes
    parsing:
      series: guessit
    seen: no
    from_digest:
      list: purge_series
    require_field:
      - series_id
      - series_name
      - series_season
    list_add:
      - entry_list: exists_series
    if:
      - "rejected_by == 'exists_series'": accept
    notify:
      entries:
        title: '* Reject copy episode: {{series_name}} - {{series_id}}'
        message: |
          Task: {{task}}
          Id: {{id|default(title)}}
          Episode: {{series_id}}
          Filename: {{filename}}
          Location: {{location}}
          Reason: {{reason|default('Unknow')}}
          Status: approved
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  # tasks to purge/clean (hard) per week
  purge-torrents-primary:
    priority: 15
    template:
      - transmission-purge
    from_transmission:
      <<: *transmission-primary
    if:
      - transmission_ratio >= 4.0: accept
      - transmission_progress == 100 and (transmission_date_done + timedelta(days=20)) < now: accept
    transmission:
      <<: *transmission-primary
      action: purge

  purge-torrents-secondary:
    priority: 15
    template:
      - transmission-purge
    from_transmission:
      <<: *transmission-secondary
    if:
      - transmission_ratio >= 4.0: accept
      - transmission_progress == 100 and (transmission_date_done + timedelta(days=10)) < now: accept
    transmission:
      <<: *transmission-secondary
      action: purge

  # task to clean (soft) daily
  clean-watchlist-movies:
    priority: 20
    seen: no
    trakt_lookup: yes
    from_digest:
      list: clean_watchlist
    accept_all: yes
    crossmatch:
      from:
        - movie_list: watchlist_movies
      fields:
        - imdb_id
      action: accept
    require_field:
      - imdb_id
    list_remove:
      - movie_list: watchlist_movies
      - trakt_list:
          account: '{? accounts.trakt_primary.username ?}'
          username: '{? accounts.trakt_primary.username ?}'
          list: watchlist
          type: movies
      - trakt_list:
          account: '{? accounts.trakt_secondary.username ?}'
          username: '{? accounts.trakt_secondary.username ?}'
          list: watchlist
          type: movies

  clean-watchlist-movies-anime:
    priority: 20
    seen: no
    trakt_lookup: yes
    from_digest:
      list: clean_watchlist_anime
    accept_all: yes
    crossmatch:
      from:
        - movie_list: watchlist_movies_anime
      fields:
        - imdb_id
      action: accept
    require_field:
      - imdb_id
    list_remove:
      - movie_list: watchlist_movies_anime
      - trakt_list:
          account: '{? accounts.trakt_primary.username ?}'
          username: '{? accounts.trakt_primary.username ?}'
          list: anime
          type: movies

  clean-watchlist-movies-spanish:
    priority: 20
    seen: no
    trakt_lookup: yes
    from_digest:
      list: clean_watchlist_spanish
    accept_all: yes
    crossmatch:
      from:
        - movie_list: watchlist_movies_spanish
        - movie_list: watchlist_movies_spanish_force
      fields:
        - imdb_id
      action: accept
    require_field:
      - imdb_id
    list_remove:
      - movie_list: watchlist_movies_spanish
      - movie_list: watchlist_movies_spanish_force
      - trakt_list:
          account: '{? accounts.trakt_primary.username ?}'
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: movies
      - trakt_list:
          account: '{? accounts.trakt_secondary.username ?}'
          username: '{? accounts.trakt_secondary.username ?}'
          list: spanish
          type: movies

  clean-remove-movies:
    priority: 20
    template:
      - kodi-clean
    seen: no
    accept_all: yes
    pending_list: remove_movies
    list_match:
      from:
        - pending_list: remove_movies
    list_remove:
      - entry_list: remove_movies_added
    delete:
      clean_source: 1
      along:
        extensions:
          - sub
          - srt
        subdirs:
          - Subs
    notify:
      entries:
        title: '* Movie removed: {{movie_name|default(title)}} ({{movie_year|default("--")}})'
        message: |
          Task: {{task}}
          Id: {{id|default(title)}}
          Filename: {{filename}}
          Location: {{location}}
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  clean-duplicate-movies:
    priority: 20
    template:
      - kodi-clean
    seen: no
    backlog: 5 days
    accept_all: yes
    pending_list: duplicate_movies
    unique: # ensure don't include duplicate items
      field:
        - id
        - movie_name
      action: reject
    list_match:
      from:
        - pending_list: duplicate_movies
      remove_on_match: yes
    list_remove:
      - pending_list: duplicate_movies
      - entry_list: duplicate_movies_added
    delete:
      clean_source: 1
      along:
        extensions:
          - sub
          - srt
        subdirs:
          - Subs
    notify:
      entries:
        title: '* Movie duplicate removed: {{movie_name|default(title)}} ({{movie_year|default("--")}})'
        message: |
          Id: {{id|default(title)}}
          Filename: {{filename}}
          Location: {{location}}
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  clean-duplicate-series:
    priority: 20
    template:
      - kodi-clean
    seen: no
    accept_all: yes
    backlog: 5 days
    entry_list: duplicate_series
    unique: # ensure don't include duplicate items
      field:
        - id
      action: reject
    list_remove:
      - entry_list: duplicate_series
    delete:
      clean_source: 50
      along:
        extensions:
          - sub
          - srt
        subdirs:
          - Subs
    notify:
      entries:
        title: '* Serie episode removed: {{series_name|default(title)}} - {{series_id|default(id)}}'
        message: |
          Id: {{id|default(title)}}
          Episode: {{series_id|default(title)}}
          Filename: {{filename}}
          Location: {{location}}
          Reason: {{duplicate_reason|default('Duplicated')}}
        via:
          - xmpp:
              sender: '{? xmpp.sender ?}'
              password: '{? xmpp.password ?}'
              recipients: '{? xmpp.recipients ?}'

  # Manual operations
  job_organize_movies:
    manual: yes
    seen: no
    template:
      - lookup-movies
    metainfo_movie: yes
    accept_all: yes
    <<: *category-movies-filter
    require_field:
      - movie_name
      - movie_year
    filesystem:
      path:
        - '{? paths.movies ?}/spanish'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    move:
      to: "{? paths.movies ?}/{{ category|default('general')|lower }}/{{ trakt_movie_name|default(movie_name) }} ({{ movie_year }})"
      rename: "{{ trakt_movie_name|default(movie_name) }} ({{ movie_year }}){% if quality|default(False) %}{% set screen_size, format, video_codec, audio_codec = screen_size|default(quality.resolution), format|default(quality.source), video_codec|default(quality.codec), audio_codec|default(quality.audio) %}{% endif %}{% if screen_size|default(False) %}[{{ screen_size }}]{% endif %}{% if format|default(False) %}.{{format|capitalize}}{% endif %}{% if video_codec|default(False) %}.{{video_codec}}{% endif %}{% if audio_codec|default(False) %}.{{audio_codec|upper}}{% endif %}"
      allow_dir: yes
      clean_source: 2
      along:
        subdirs:
          - Sub
          - Subs
          - Extra
          - Extras
        extensions:
          - srt
          - png
          - jpg

  job_purge-watchlist-movies:
    manual: yes
    seen: no
    template:
      - lookup-movies
    metainfo_movie: yes
    filesystem:
      path:
        - '{? paths.movies ?}'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    list_match:
      from:
        - movie_list: watchlist_movies
    require_field:
      - movie_name
      - movie_year
    list_remove:
      - movie_list: watchlist_movies
      - trakt_list:
          account: '{? accounts.trakt_primary.username ?}'
          username: '{? accounts.trakt_primary.username ?}'
          list: watchlist
          type: movies
      - trakt_list:
          account: '{? accounts.trakt_secondary.username ?}'
          username: '{? accounts.trakt_secondary.username ?}'
          list: watchlist
          type: movies

  job_purge-watchlist-movies-spanish:
    manual: yes
    seen: no
    template:
      - lookup-movies
    metainfo_movie: yes
    filesystem:
      path:
        - '{? paths.spanish_movies ?}'
        - '{? paths.movies ?}/family'
        - '{? paths.movies ?}/pg'
        - '{? paths.movies ?}/pg13'
        - '{? paths.movies ?}/allages'
        - '{? paths.movies ?}/anime_spanish'
      regexp: '.*\.(avi|mkv|mp4)$'
      recursive: yes
    list_match:
      from:
        - movie_list: watchlist_movies_spanish
        - movie_list: watchlist_movies_spanish_force
    require_field:
      - movie_name
      - movie_year
    list_remove:
      - movie_list: watchlist_movies_spanish
      - movie_list: watchlist_movies_spanish_force
      - trakt_list:
          account: '{? accounts.trakt_primary.username ?}'
          username: '{? accounts.trakt_primary.username ?}'
          list: spanish
          type: movies
      - trakt_list:
          account: '{? accounts.trakt_secondary.username ?}'
          username: '{? accounts.trakt_secondary.username ?}'
          list: spanish
          type: movies

  job_purge-exists-series:
    manual: yes
    seen: no
    seen_info_hash: no
    metainfo_series: yes
    trakt_lookup: yes
    parsing:
      series: guessit
    template:
      - transmission-purge
    from_transmission:
      <<: *transmission-secondary
    crossmatch:
      from:
        - entry_list: exists_series
      fields:
        - series_id
        - series_name
        - series_season
      action: accept
      all_fields: yes
    list_remove:
      - entry_list: exists_series
    transmission:
      <<: *transmission-secondary
      action: purge

## schedules for daemon mode
schedules:
  - tasks: [watchlist-*] #, collection-*]
    interval:
      hours: 1
  - tasks: [subtitles-get, copy-*]
    interval:
      minutes: 30
  - tasks: [download-*, clean-*]
    interval:
      hours: 6
    #schedule:
    #  hour: 2,6,18
  - tasks: [copy-*, filter-*, purge-*]
    schedule:
      day_of_week: tue,fri
      hour: 13

web_server:
  bind: 0.0.0.0
  port: 3539
  web_ui: yes
  base_url: /
